package my.ezbart;

import java.util.HashMap;

import org.xmlvm.iphone.NSDate;

/**
 * @author Cecil Holmes
 * @version 0.1
 * 
 * DateParser.java
 * 
 * Helper class that receives an NSDate object as input and converts it to a String object
 * with the proper time corresponding to the Pacific Time Zone.
 */
public class DateParser {
	private HashMap<String, String> monthName = new HashMap<String, String>();
	private String fullDate;
	private String month;
	private String day;
	private String hour;
	private String minute;
	private String amPm;
	
	public DateParser(NSDate inputDate) {
		double conversion = inputDate.timeIntervalSinceReferenceDate() - 28800;
		inputDate = NSDate.dateWithTimeIntervalSinceReferenceDate(conversion);
		fullDate = inputDate.toString();
		initMonthMap();
		month = monthName.get(fullDate.substring(5,7));
		day = Integer.toString(Integer.parseInt(fullDate.substring(8,10)));
		hour = fullDate.substring(11,13);
		minute = fullDate.substring(14,16);
		setAmPm();
	}

	private void setAmPm() {
		int hourVal = Integer.parseInt(hour);
		if (hourVal<=12) {
			amPm = "AM";
			if (hourVal == 0) {
				hour = "12";
			}
		} else {
			amPm = "PM";
			hour = Integer.toString(hourVal-12);
		}
	}
	
	private void initMonthMap() {
		monthName.put("01", "Jan");
		monthName.put("02", "Feb");
		monthName.put("03", "Mar");
		monthName.put("04", "Apr");
		monthName.put("05", "May");
		monthName.put("06", "Jun");
		monthName.put("07", "Jul");
		monthName.put("08", "Aug");
		monthName.put("09", "Sep");
		monthName.put("10", "Oct");
		monthName.put("11", "Nov");
		monthName.put("12", "Dec");
	}
	
	public String getDate() {
		return (month + " " + day + ", " + hour + ":" + minute + " " + amPm);
	}
}
